package cn.erp.app.util;

import java.util.List;

import com.jfinal.plugin.activerecord.Model;

public class SQLCondition<M extends Model<M>> {
	public static String EQ = "=";
	public static String UNEQ = "!=";
	public static String ASC = "asc";
	public static String DESC = "desc";
	private M m;
	private String tableName;
	private String space = " ";
	private String selectSql="";
	private String whereSql = "where";
	private String orderSql = "";
	private int conditionNum = 0;
	private int selectNum = 0;
	public SQLCondition(String tableName,M m) {
		this.m = m;
		this.tableName = tableName;
	}
	
	/**
	 * 
	 * @param fieldName 字段名称
	 * @param value 字段值，为null则不添加
	 * @param connSymbol 条件之间的连接符，若只有一个，可为null
	 * @param match 条件匹配符
	 */
	public void addCondition(String fieldName,Object value,String connSymbol,String match) {
		if(value==null) {
			return;
		}
		if(conditionNum>0) {
			whereSql+=space+connSymbol+match+value;
		} else {
			whereSql+=space+match+value;
		}
		
	}
	
	public void addSelect(String fieldName,String alias) {
		if(selectNum>0) {
			selectSql+=",";
		}
		selectSql +=fieldName+" as "+alias;
	}
	
	public void addOrder(String[] fields,String sort) {
	}
	
	public List<M> getResult() {
		String sql = "select"+space+selectSql+space+"from"+space+tableName+space+whereSql;
		System.out.println("执行sql:"+sql);
		return this.m.find(sql);
	}
}
